package objects;

import java.io.IOException;
import java.util.ArrayList;
import objects.slots.ArraySlot;
import objects.slots.ListSlot;
import objects.slots.Slot;
import objects.visualization.Block;
import objects.visualization.ListBlock;
import objects.visualization.TextBlock;

public class Method extends StaticObject {
	public ArrayList<Field> parameters = new ArrayList<Field>();
	public ArrayList<Field> localVariables = new ArrayList<Field>();
	public ArrayList<Module> modules = new ArrayList<Module>();
	public GruClass returnValueClass;

	public Method() {
		this.gruClass = methodClass;
	}
	
	@Override
	public void io() throws IOException {
		iD = stringIO( iD );
		returnValueClass = objectIO( returnValueClass );
		isStatic = booleanIO( isStatic );
		listIO( parameters );
		listIO( localVariables );
		listIO( modules );
	}
	
	@Override
	public DataObject newObject() {
		return new Method();
	}

	String headerCode() {
		return ( isStatic ? "static " :  "" ) + ( returnValueClass == null ? "void" : returnValueClass.iD ) + iD;
	}

	Block toBlock() {
		ListBlock block = new ListBlock( true, false );
		
		ListBlock header = new ListBlock( false, false );
		header.add( new TextBlock( this, headerCode() + "( ", 1 ) );
		boolean first = true;
		for( Field parameter : parameters ) {
			if( !first ) header.add( new TextBlock( null, ", " ) );
			header.add( parameter.toBlock() );
			first = false;
		}
		header.add( new TextBlock( null, " )" ) );
		block.add( header );
		
		block.add( new Block( new ListSlot( modules, 0, Slot.Position.BEFORE ) ) );
		for( int n = 0; n < modules.size(); n++ ) {
			block.add( modules.get( n ).toModuleBlock( 2 ) );
			block.add( new Block( new ListSlot<Module>( modules, n, Slot.Position.AFTER ) ) );
		}
		
		return block;
	}
}
